﻿#region Copyrigth and License
/*==============================================================================
 *  Copyright (c) cndotnet.org Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.cndotnet.org/ucenter
 * ==============================================================================*/
#endregion
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Linq.Dynamic;
using UCenter.Core.Repository;
using System;
using System.Linq.Expressions;

namespace UCenter.SQLServerProvider
{
    public class SqlRepositoryBase<T>:RepositoryBase where T:class
    {
        #region Fileds

        #endregion

        #region Constructors

        #endregion

        #region Propertity
        #endregion

        #region Events

        #endregion

        #region Methods
        public T Add(T obj)
        {
            using (RepositoryContext db = new RepositoryContext())
            {
                db.GetTable<T>().InsertOnSubmit(obj);
                db.SubmitChanges();
                return obj;
            }
        }
        public T GetSingle(string filter, DataLoadOptions loadOptions, params object[] values)
        {
            using (RepositoryContext db = new RepositoryContext())
            {        
                if (loadOptions != null)
                {
                    db.LoadOptions = loadOptions;
                }
                var data = db.GetTable<T>().Where<T>(filter, values).SingleOrDefault<T>();
                return data;
            }
        }

        public T GetSingle(Expression<Func<T, bool>> predicate)
        {
            using (RepositoryContext db = new RepositoryContext())
            {
                var data = db.GetTable<T>().Where(predicate).SingleOrDefault<T>();
                return data;
            }
        }


        public List<T> GetList(string filter, DataLoadOptions loadOptions, params object[] values)
        {
            using (RepositoryContext db = new RepositoryContext())
            {
                if (loadOptions != null)
                {
                    db.LoadOptions = loadOptions;
                }
                var datas = db.GetTable<T>().Where<T>(filter, values);
                return datas.ToList<T>();
            }
        }
        #endregion
    }
}
